package com.iscreen.dbupgrade.operations;

import com.iscreen.dbupgrade.DBUpgrade;
import com.iscreen.utils.db.connection.DBConnection;
import com.iscreen.utils.exceptions.IScreenException;

public abstract class UpgradeOperation
{

	protected abstract void doExecute() throws IScreenException;

	public abstract long getStepId();

	public abstract long getCommandId();

	public abstract String getOperation();

	public abstract String getSql();

	protected DBConnection connection;

	protected final OperationsHandler operationsHandler = DBUpgrade
			.getInstance().getFactory().getOperationsHandler();

	public void execute() throws IScreenException
	{
		if (!isDone())
		{
			markStart();
			doExecute();
			markDone();
		}
	}

	private boolean isDone() throws IScreenException
	{
		return operationsHandler.isDone(this);
	}

	private void markStart() throws IScreenException
	{
		operationsHandler.markStart(this);
	}

	private void markDone() throws IScreenException
	{
		operationsHandler.markDone(this);
	}

	public DBConnection getConnection()
	{
		return connection;
	}
}
